package hu.plajko.server.jdo;

import hu.plajko.shared.rpc.NotificationRPC;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Transient;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class Notification {

	public Notification() {

	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;

	@Persistent
	private Text text;

	@Persistent
	private int top;
	@Persistent
	private int left;
	@Persistent
	private int width;
	@Persistent
	private int height;
	@Persistent
	private int zindex;

	public Text getText() {
		return text;
	}

	public void setText(Text text) {
		this.text = text;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	public int getLeft() {
		return left;
	}

	public void setLeft(int left) {
		this.left = left;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	@Transient
	public void setId(Key id) {
		this.id = id;
	}

	@Transient
	public Key getId() {
		return id;
	}

	public static Notification fromRPC(NotificationRPC nr) {
		System.out.println("fromRPC: " + nr.toString());
		Notification n = new Notification();
		n.setId(KeyFactory.stringToKey(nr.getId()));
		n.setText(new Text(nr.getText()));
		n.setLeft(nr.getLeft());
		n.setTop(nr.getTop());
		n.setWidth(nr.getWidth());
		n.setHeight(nr.getHeight());
		n.setZindex(nr.getZindex());
		return n;
	}

	public static NotificationRPC toRPC(Notification n) {
		System.out.println("toRPC: " + n.toString());
		NotificationRPC nr = new NotificationRPC();
		nr.setId(KeyFactory.keyToString(n.getId()));
		nr.setText(n.getText() == null ? null : n.getText().getValue());
		nr.setLeft(n.getLeft());
		nr.setTop(n.getTop());
		nr.setWidth(n.getWidth());
		nr.setHeight(n.getHeight());
		nr.setZindex(n.getZindex());
		return nr;
	}

	@Override
	public String toString() {
		return "Notification [height=" + height + ", id=" + id + ", left=" + left + ", text=" + text + ", top=" + top + ", width=" + width + "]";
	}

	public void setZindex(int zindex) {
		this.zindex = zindex;
	}

	public int getZindex() {
		return zindex;
	}

}
